//
//  YYAppConfig.swift
//  YYProject
//
//  Created by 陆文宇 on 2017/8/10.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import RealmSwift

class YYAppConfig : Object {
    @objc dynamic var key = ""
    @objc dynamic var value = ""
    
    override class func primaryKey() -> String? {
        return "key"
    }
    
    class func set(key: String, value: String) {
        let realm = YYRealm.initialize()
        
        let appConfig = YYAppConfig()
        appConfig.key = key
        appConfig.value = value
        
        realm.beginWrite()
        realm.add(appConfig, update: true)
        try! realm.commitWrite()
    }
    
    class func getOne(key: String) -> String? {
        let realm = YYRealm.initialize()
        let items = realm.objects(self).filter("key = '\(key)'")
        
        if items.count > 0 {
            return items[0].value
        } else {
            return nil
        }
    }
    
    class func deleteOne(key: String) {
        let realm = YYRealm.initialize()
        let configLists = realm.objects(self).filter("key == '\(key)'")
        
        realm.beginWrite()
        realm.delete(configLists)
        try! realm.commitWrite()
    }
}
